package com.example.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * 数据源配置类
 * @author 李平
 * 2022-4-30
 */
@Configuration
@EnableConfigurationProperties
public class DataSourceConfig {

    @Bean(name = "myDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    public DataSource myDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "apiJdbcTemplate")
    public JdbcTemplate getTemplate(@Qualifier("myDataSource") DataSource teseDataSource) {
        return new JdbcTemplate(teseDataSource);
    }

}
